﻿// See https://aka.ms/new-console-template for more information

#region 隐式转换,同类型,小范转化成大范围
//long->int->short->sbyte ;double -> float
//decimal和特殊类型bool char string 不存在隐式转换.
int i = 1;
long b = i;
Console.WriteLine(b +"c的类型");


#endregion

#region 不同类型的隐式转换
//高精度(大范围)->低精度(小范围)
//double->float ->整型(无符号,有符号)->char
//decimal ->整型(无符号,有符号)->char
//string 和bool 不能转换
#endregion

#region 显式转换
#region 括号强转
int i2 = -1;

//高精度转低精度
//浮点转成int.会直接丢掉后面小数.没有四舍五入

//负数转uint 会溢出.
/*uint ui = (uint)i2;
Console.WriteLine(ui);*/

short s2 = (short)i2;
Console.WriteLine(s2);
double d = 1.9;
i2 = (int)d;
Console.WriteLine(i2);

char c2 = 'A';
Console.WriteLine((int)c2);
#endregion

#region parse
//parse,{变量类型}.parse("字符串")
string str2 = "123";
int i3 = int.Parse(str2);
long l2 = long.Parse("1234567890");
double d2 = double.Parse("123.15");
float f2 = float.Parse("1.2");

Console.WriteLine(i3);
//Conver
#endregion

#region Convert
//有四舍五入.浮点数到int
int a = Convert.ToInt32("15");
Console.WriteLine(a);
a = Convert.ToInt32(98.6f);
Console.WriteLine(a.GetType()+",a="+a);
char ch = 'A';

Console.WriteLine(Convert.ToChar(a));
Console.WriteLine(a);
Console.WriteLine(Convert.ToInt32(ch));

#endregion

#region tostring
//其他类型转string
string s = i2.ToString();
//基本类型会自动转调用tostring 转化.
Console.WriteLine(s+1.2+true);
#endregion
#endregion